package lt.ntec.danblog.server.core;

import lt.ntec.danblog.server.core.model.User;
import lt.ntec.danblog.server.core.model.UserDAO;
import com.google.appengine.api.users.UserService;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class GoogleUserManager implements UserManager {

    private UserDAO userDao;
    private UserService userService;
    private com.google.appengine.api.users.User googleUser;

    @Inject
    GoogleUserManager(UserDAO userDao, UserService userService) {
        this.userDao = userDao;
        this.userService = userService;
    }

    @Override
    public User getCurrentUser() throws UserNotFoundException {
        if (!isAvailableValidUser()) {
            return null;
        }
        String email = userService.getCurrentUser().getEmail();
        return userDao.getUserByEmail(email);
    }

    @Override
    public boolean isAvailableValidUser() {
        return userService.isUserLoggedIn();
    }

    @Override
    public User getUser(Long id) {
        return userDao.getById(id);
    }

    @Override
    public String getLoginUrl(String destination) {
        return userService.createLoginURL(destination);
    }

    @Override
    public String getLogoutUrl(String destination) {
        throw new UnsupportedOperationException("Not working");
    }
}
